﻿-----------------------------------------------------------------------
--Options Panel for Classic ThreatMeter
-----------------------------------------------------------------------
-- Options panel is in the middle of a code cleanup
-- Bit of old code mixed with new.
local L = LibStub("AceLocale-3.0"):GetLocale("ClassThreatMeter")
--FOR XML locale
BTMOPTTITLE = L["Blz仇恨监视器"]
BTMOPTDESC = L["动态显示你的目标与焦点目标的仇恨值。基于原Blz仇恨监视器而开发。"]
BTMOPTWIDE = L[" 宽度"]
BTMOPTBARS = L[" 条"]
BTMOPTTBS = L[" 仇恨条"]

-- Tables for Options Panel Text
local Opt_Threat_Name = {L["简易仇恨"], L["百分比"], L["仇恨值 (短写)"], L["仇恨值 (长写)"], L["每秒仇恨值（TPS）"], L["每分仇恨值（TPM）"], L["未知"],
	[0] = L["禁用"]
}
local Opt_Threat_Text = {L["坦克"], L["获取"], L["失去"], L["安全"], L["安全"], "100%", "95%", "90%", "80%", "60%", "65.50K", "62.20K", "59.00K", "52.40K", "39.30K", "65500", "62200", "59000", "52400", "39300", "4.36K TPS", "4.15K TPS", "3.93K TPS", "3.49K TPS", "2.62K TPS", "23.81K TPM", "22.62K TPM", "21.45K TPM", "19.05K TPM", "14.29K TPM", L["错误"]}

function ClassicThreatMeterPanel_OnLoad(self) --default: panel
	local prefix = self:GetName();
	pname = self:GetName(); --easier calls	
	self.pet = _G[prefix.."_PET"];
	self.pet.text = _G[prefix.."_PETText"];
	self.port = _G[prefix.."_PORT"];
	self.port.text = _G[prefix.."_PORTText"];
	self.bckdrp = _G[prefix.."_BCKDRP"];
	self.bckdrp.text = _G[prefix.."_BCKDRPText"];
	--self.style = _G[prefix.."_Style"];
	--self.style.text = _G[prefix.."_StyleText"];
	self.threatdd = _G[prefix.."_ThreatBarDropDown"];
	self.threatdd.label = _G[prefix.."_ThreatBarDropDownLabel"];
	self.threatdd.text = _G[prefix.."_ThreatBarDropDownText"];
	self.tooltipdd = _G[prefix.."_ThreatToolTipDropDown"];
	self.tooltipdd.label = _G[prefix.."_ThreatToolTipDropDownLabel"];
	self.tooltipdd.text = _G[prefix.."_ThreatToolTipDropDownText"];
	self.sliderX = _G[prefix.."_ThreatBarSliderX"];
	self.sliderN = _G[prefix.."_ThreatBarSliderNUM"];
	self.statusbar = _G[prefix.."_Statusbar"];

	--Register Events
	self:RegisterEvent("ADDON_LOADED");

	self.name = L["Blz仇恨监视器"];
	self.okay = function (self) ClassicThreatMeterPanel_Close(self); end;
	self.cancel = function (self)  ClassicThreatMeterPanel_CancelOrLoad(self);  end;
	InterfaceOptions_AddCategory(self);

	ClassicThreatMeterPanel_FrameSetup(self);

end

function ClassicThreatMeterPanel_OnEvent(self, event)
	if ( event == "ADDON_LOADED" ) then
		--Fixes the "AM" Easy Threat Issue
		if ( ThreatMeter_Saved.OPT_INLINE == 0 ) then
			self.threatdd.text:SetText(L["禁用"]);
		elseif (ThreatMeter_Saved.OPT_INLINE == 1 ) then
			self.threatdd.text:SetText(L["简易显示"]);
		end
		if ( ThreatMeter_Saved.OPT_TIP == 0) then
			self.tooltipdd.text:SetText(L["禁用"]);
		elseif ( ThreatMeter_Saved.OPT_TIP == 1) then
			self.tooltipdd.text:SetText(L["简易显示"]);
		end

		ClassicThreatMeterPanel_CancelOrLoad(self);
	end
-----------------------------------------------------------------------
end

function ClassicThreatMeterPanel_Close(self)
-- Save these variables in live + temp
OPTPET = self.pet:GetChecked();
OPTPORT = self.port:GetChecked();
OPTBCKDRP = self.bckdrp:GetChecked();
OPTINLINE = UIDropDownMenu_GetSelectedValue(self.threatdd);
OPTTIP = UIDropDownMenu_GetSelectedValue(self.tooltipdd);
OPTSIZEX  = self.sliderX:GetValue();
NUMTHREATBARS = self.sliderN:GetValue();

-- Dump Data (Prep for logout/exit)
ThreatMeter_Saved.OPT_PET = OPTPET
ThreatMeter_Saved.OPT_PORT = OPTPORT
ThreatMeter_Saved.OPT_TIP = OPTTIP
ThreatMeter_Saved.OPT_INLINE = OPTINLINE
ThreatMeter_Saved.OPT_SIZE_X = OPTSIZEX
ThreatMeter_Saved.NUM_THREAT_BARS = NUMTHREATBARS
ThreatMeter_Saved.OPT_BCKDRP = OPTBCKDRP
end

function ClassicThreatMeterPanel_CancelOrLoad(self)
-- Revert to saved
self.pet:SetChecked(ThreatMeter_Saved.OPT_PET);
self.port:SetChecked(ThreatMeter_Saved.OPT_PORT);
self.bckdrp:SetChecked(ThreatMeter_Saved.OPT_BCKDRP);
self.sliderX:SetValue(ThreatMeter_Saved.OPT_SIZE_X);
self.sliderN:SetValue(ThreatMeter_Saved.NUM_THREAT_BARS);

UIDropDownMenu_SetSelectedValue(self.threatdd, ThreatMeter_Saved.OPT_INLINE);
UIDropDownMenu_SetSelectedValue(self.tooltipdd, ThreatMeter_Saved.OPT_TIP);

OPTPET = ThreatMeter_Saved.OPT_PET
OPTPORT = ThreatMeter_Saved.OPT_PORT
OPTBCKDRP = ThreatMeter_Saved.OPT_BCKDRP
OPTTIP = ThreatMeter_Saved.OPT_TIP
OPTINLINE = ThreatMeter_Saved.OPT_INLINE
OPTSIZEX  = ThreatMeter_Saved.OPT_SIZE_X
NUMTHREATBARS = ThreatMeter_Saved.NUM_THREAT_BARS

-- load and set dynabar values
ClassicThreatMeterPanel_DemoThreatHead:SetWidth(ThreatMeter_Saved.OPT_SIZE_X);
ClassicThreatMeterPanel_DemoThreatHeadName:SetWidth(ThreatMeter_Saved.OPT_SIZE_X-8);
	for i=1,5 do
		statusbarwidth = getglobal(pname.."_StatusBar"..i);
		statusbarwidth:SetWidth(ThreatMeter_Saved.OPT_SIZE_X-10);
	end

--Reset the Demo
ClassicThreatMeterPanel_UpdateDemoCLK();
ClassicThreatMeterPanel_ThreatDisplay(ThreatMeter_Saved.OPT_INLINE);
end

function ClassicThreatMeterPanel_UpdateDemoX(self, size)
	--capture values and display [Not Saved]
	ClassicThreatMeterPanel_DemoThreatHead:SetWidth(size);
	ClassicThreatMeterPanel_DemoThreatHeadName:SetWidth(size-8);
	for i=1, 5 do
		local statusbarI = getglobal(pname.."_StatusBar"..i);
		statusbarI:SetWidth(size-10);
	end
end

function ClassicThreatMeterPanel_UpdateDemoNUM(count)
	if ( count == 3 ) then
		ClassicThreatMeterPanel_StatusBar4:Hide();
		ClassicThreatMeterPanel_StatusBar5:Hide();
	elseif (count == 4 ) then
		ClassicThreatMeterPanel_StatusBar4:Show();
		ClassicThreatMeterPanel_StatusBar5:Hide();
	else
		ClassicThreatMeterPanel_StatusBar4:Show();
		ClassicThreatMeterPanel_StatusBar5:Show();
	end
end

function ClassicThreatMeterPanel_UpdateDemoCLK()
--local prefix = getglobal(self:GetName());
local statusbarpet = getglobal(pname.."_StatusBar4Icon");
local portdir = "Interface\\CHARACTERFRAME\\TemporaryPortrait-";

	if ( ClassicThreatMeterPanel_PET:GetChecked() == 1 ) then
		statusbarpet:SetTexture(portdir.."PET");
		statusbarpet:SetTexCoord(0, 1, 0, 1);
		statusbarpet:SetVertexColor(1.0, 1.0, 1.0);
		statusbarpet:Show();
	elseif ( ClassicThreatMeterPanel_PET:GetChecked() == nil ) then
		statusbarpet:SetTexture("Interface\\Minimap\\ObjectIcons");
		statusbarpet:SetTexCoord(0.75, 0.875, 0.5, 1.0);
		statusbarpet:SetVertexColor(0.78, 0.61, 0.43);
		statusbarpet:Show();
	end

	if ( ClassicThreatMeterPanel_PORT:GetChecked() == 1 ) then

		for i=1, 5 do
			if (i == 4) then i=5 end
				local statusbaricon = getglobal(pname.."_StatusBar"..i.."Icon");
			--PLAYER
			if (i == 1) then
				SetPortraitTexture(statusbaricon, "player");
			elseif (i == 2) then
				statusbaricon:SetTexture(portdir.."Male-Gnome");
			elseif (i == 3) then
				statusbaricon:SetTexture(portdir.."Male-Nightelf");
			elseif (i == 5) then
				statusbaricon:SetTexture(portdir.."Male-Draenei");
			end
			statusbaricon:SetTexCoord(0, 1, 0, 1);
			statusbaricon:SetVertexColor(1.0, 1.0, 1.0);
			statusbaricon:Show();
		end
	elseif ( ClassicThreatMeterPanel_PORT:GetChecked() == nil ) then
		for i=1, 5 do
			if (i == 4) then i=5 end
			local statusbaricon = getglobal(pname.."_StatusBar"..i.."Icon");
				statusbaricon:SetTexture("Interface\\Minimap\\ObjectIcons");
				statusbaricon:SetTexCoord(0.75, 0.875, 0.5, 1.0);
				if (i == 1) then
					local class, fileName = UnitClassBase("player");
					local color = RAID_CLASS_COLORS[fileName];
					statusbaricon:SetVertexColor(color.r, color.g, color.b);
				elseif (i == 2) then
					statusbaricon:SetVertexColor(0.41, 0.8 , 0.94);
				elseif (i == 3) then
					statusbaricon:SetVertexColor(0.67, 0.83, 0.45);
				elseif (i == 5) then
					statusbaricon:SetVertexColor(0.14, 0.35, 1.0);
				end
			statusbaricon:Show();
		end
	end

	if ( ClassicThreatMeterPanel_BCKDRP:GetChecked() == 1 ) then
		for i=1, 5 do
			local statusbarI = getglobal(pname.."_StatusBar"..i);
			statusbarI:SetBackdropColor(0.2,0.2,0.2,0.8);
		end
	else
		for i=1, 5 do
			local statusbarI = getglobal(pname.."_StatusBar"..i);
			statusbarI:SetBackdropColor(0.2,0.2,0.2,0.0);
		end
	end
end

function ClassicThreatMeterPanel_FrameSetup(self)
--Preset threat colors
local C1 = 0.99999780301005
local C2 = 0.59999868180603
local C3 = 0.46666564140469
local C4 = 0.69019456207752 --Boom?
local C5 = 0.0
local panelmodel = getglobal(pname.."_Model");
local panelshadow = getglobal(pname.."_ModelShadow");

	--Setup my signature model (acting as filler until I expand)
	panelmodel:ClearModel();
	panelmodel:SetModel("Creature\\Miev\\Miev.m2");
	panelmodel:SetLight(1, 0, 0, -0.6, -0.6, 1.0, 1.0, 1.0, 1.0, 0.8, 1.0, 1.0, 1.0);
	panelmodel:SetCamera(1);
	panelmodel:SetSequence(4);
	panelshadow:ClearModel();
	panelshadow:SetModel("Creature\\Miev\\Miev.m2");
	panelshadow:SetCamera(1);
	panelshadow:SetSequence(4); 

	-- Set Option Selection Text
	self.pet.text:SetText(L["显示宠物头像"]);
	self.port.text:SetText(L["显示玩家头像"]);
	self.bckdrp.text:SetText(L["显示仇恨条背景"]);
	self.threatdd.label:SetText(L["仇恨条显示样式"]);
	self.tooltipdd.label:SetText(L["提示显示样式"]);

	--Preset value min/max (simple loop added)
	-- and demo values
	for i=1, 5 do
		local statusbarval = getglobal(pname.."_StatusBar"..i);
		statusbarval:SetMinMaxValues(0, 100);
		statusbarval:SetValue((100-((i*5)-5)));
		if (i == 4) then statusbarval:SetValue(80);
		elseif (i == 5) then statusbarval:SetValue(60); end 
		--math fix
	end

	--Values are logged from the server (WoTLK 3.0.2)
	--Set them for preview mode
	ClassicThreatMeterPanel_DemoThreatHead:SetBackdropBorderColor(C1, C5, C5);
	ClassicThreatMeterPanel_DemoThreatHead:SetBackdropColor(C1, C5, C5);
	ClassicThreatMeterPanel_StatusBar1:SetStatusBarColor(C1, C5, C5);
	ClassicThreatMeterPanel_StatusBar2:SetStatusBarColor(C1, C2, C5);
	ClassicThreatMeterPanel_StatusBar3:SetStatusBarColor(C1, C1, C3);
	ClassicThreatMeterPanel_StatusBar4:SetStatusBarColor(C4, C4, C4);
	ClassicThreatMeterPanel_StatusBar5:SetStatusBarColor(C4, C4, C4);

	-- Set Preview Names
	ClassicThreatMeterPanel_DemoThreatHeadName:SetText(L["目标或焦点目标"]);
	ClassicThreatMeterPanel_StatusBar1Name:SetText(UnitName("player"));
	ClassicThreatMeterPanel_StatusBar2Name:SetText("Parsous");
	ClassicThreatMeterPanel_StatusBar3Name:SetText("Fordprefect");
	ClassicThreatMeterPanel_StatusBar4Name:SetText("MrBear");
	ClassicThreatMeterPanel_StatusBar5Name:SetText("Ardenius");

end

--StatusBar Threat Dropdown
function ClassicThreatMeterPanel_ThreatBarDropDown_Initialise()
	level = level or 1

	local info = UIDropDownMenu_CreateInfo();
	UIDropDownMenu_SetWidth(ClassicThreatMeterPanel_ThreatBarDropDown, 128)
 	for i=0, 6 do
		info.text = Opt_Threat_Name[i];
		info.value = i;
		info.func = function() ClassicThreatMeterPanel_ThreatBarDropDown_OnClick() end;
		info.owner = this:GetParent();
		info.checked = nil;
		info.icon = nil;
		UIDropDownMenu_AddButton(info, level);
	end
end

function ClassicThreatMeterPanel_ThreatBarDropDown_OnClick() 	UIDropDownMenu_SetSelectedValue(ClassicThreatMeterPanel_ThreatBarDropDown, this.value);
	ClassicThreatMeterPanel_ThreatDisplay(this.value);
end

function ClassicThreatMeterPanel_ThreatDisplay(selection)
	local prefix = "ClassicThreatMeterPanel_StatusBar";
	if (selection == 0 or selection == nil) then
		for i=1,5 do
			local StatusBarNThreat = getglobal(prefix..i.."Threat");
			StatusBarNThreat:SetText(nil);
		end
	elseif (selection >= 1) then
		for i=1,5 do
			local StatusBarNThreat = getglobal(prefix..i.."Threat");
			StatusBarNThreat:SetText(Opt_Threat_Text[i+((selection*5)-5)]);
		end
	else
		for i=1,5 do
			local StatusBarNThreat = getglobal(prefix..i.."Threat");
			StatusBarNThreat:SetText(Opt_Threat_Text[32]);
		end
 	end
end

--ToolTip Dropdown options
function ClassicThreatMeterPanel_ThreatToolTipDropDown_Initialise()
	level = level or 1
 
	local info = UIDropDownMenu_CreateInfo();
	UIDropDownMenu_SetWidth(ClassicThreatMeterPanel_ThreatToolTipDropDown, 128)
	for i=0, 6 do
		info.text = Opt_Threat_Name[i];
		info.value = i;
		info.func = function() ClassicThreatMeterPanel_ThreatToolTipDropDown_OnClick() end;
		info.owner = this:GetParent();
		info.checked = nil;
		info.icon = nil;
		UIDropDownMenu_AddButton(info, level);
	end
end

function ClassicThreatMeterPanel_ThreatToolTipDropDown_OnClick() 	UIDropDownMenu_SetSelectedValue(ClassicThreatMeterPanel_ThreatToolTipDropDown, this.value);
end

wsRegisterButton(
	WS_CONFIG_SEC_1,
	"Raid",
	L["仇恨监视器"],
	L["仇恨监视器"],
	L["在目标,焦点旁显示与之相关的前5~10个仇恨信息"],
	"Interface\\Icons\\Spell_Holy_Testoffaith",
	function()
		InterfaceOptionsFrame_OpenToCategory(L["Blz仇恨监视器"]);
	end
);